package subscribersManagement;

import hibernateUtils.HibernateUtil;

import java.util.List;

import mediasManagement.Media;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import exceptions.*;
import subscribersManagement.Subscriber;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;


public class SubscriberDAO {

	/** 
	 * Tests the presence of any subscriber in DB
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean isEmpty(){
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from subscribers");
		List<Media> medias = query.list();
		if (medias.isEmpty())
			return true;
		else
			return false;	
	}

	/**
	 * Adds a subscriber in the persistence unit (hence in DB)
	 * 
	 * @param subscriber
	 * @return
	 */
	public static boolean add(Subscriber subscriber){
		Session session= HibernateUtil.openSession();
		try{
			for(Entitled entitle: subscriber.getEntitles()){
				session.save(entitle);
			}
			session.save(subscriber);
			System.out.println("ajout effectue");
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
	
		return true;
	}

	/**
	 * Removes a subscriber in the persistence unit (hence in DB)
	 * 
	 * @param subscriber
	 * @return
	 */
	public static boolean remove(Subscriber subscriber){
		Session session= HibernateUtil.openSession();
		try{
			for(Entitled entitle: subscriber.getEntitles()){
				session.delete(entitle);
			}
			session.delete(subscriber);
			System.out.println("suppression effectuee");
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
	
		return true;
	}

	public boolean contains(Subscriber subscriber) throws MediaExistsException {
		Session session = HibernateUtil.openSession();
		if (subscriber== null) throw new MediaExistsException();
		Subscriber s = (Subscriber) session.get(Subscriber.class,subscriber.getNumber());
		if (s ==null) return false;
		else return s.equals(subscriber);	
	}
	
	
	public Subscriber GetSingleSubscriberByLastName(String lastName) throws SubscriberExistsException {
		Session session = HibernateUtil.openSession();
	Criteria criteria = session.createCriteria(Subscriber.class).add(Restrictions.eq("lastName", lastName));
	Subscriber subscriber = (Subscriber)criteria.uniqueResult();
		if (subscriber == null) throw new SubscriberExistsException();
		else
			return subscriber;
	}
	
	/**
	 * Adds an entitled in the persistence unit (hence in DB)
	 * 
	 * @param entitled
	 * @return
	 */
	public boolean add(Entitled entitled){
		Session session= HibernateUtil.openSession();
		try{
			session.save(entitled);
			System.out.println("ajout effectue");
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
	
		return true;
	}

	/**
	 * Gets a subscriber.
	 * 
	 * @param subscriberId
	 * @return
	 * @throws SubscriberExistsException
	 */
//	public Subscriber get(long subscriberId) throws SubscriberExistsException {
//		EntityManager entityManager=JPAUtil.getEntityManager();
//		Subscriber s = (Subscriber) entityManager.find(Subscriber.class, subscriberId);
//		if (s ==null) throw new SubscriberExistsException();
//		else return s;
//	}
//
//	/**
//	 * Lists subscribers in DB.
//	 * 
//	 * @return
//	 */
//	public List<Subscriber> getContent(){
//		EntityManager entityManager=JPAUtil.getEntityManager();
//		return ((List<Subscriber>)entityManager.createNamedQuery("findAllSubscribers").getResultList());
//	}
//
//	/**
//	 * Gets the size of the table Subscribers.
//	 * 
//	 * @return
//	 */
//	public long size(){
//		EntityManager entityManager=JPAUtil.getEntityManager();
//		return (entityManager.createNamedQuery("findAllSubscribers").getResultList().size());
//	}
//
//	/**
//	 * Gets a subscriber by property classification
//	 * 
//	 * @param String propertyName
//	 * @param Object value
//	 * @return List
//	 * @throws DvdExistsException
//	 */
//	public static List getByProperty(String propertyName, Object value) throws DvdExistsException {
//		EntityManager entityManager=JPAUtil.getEntityManager();
//		Query query = (Query) entityManager.createQuery("select s from Subscriber s where s."+ propertyName +"= ?");
//		List l = (List) query.getResultList();
//		if (l==null) throw new DvdExistsException();
//		else return l;
//	}
//
//	/**
//	 * 
//	 */
//	public String toString(){
//		EntityManager entityManager=JPAUtil.getEntityManager();
//		String result="";
//		List<Subscriber> theSubscribers = (List<Subscriber>) entityManager.createNamedQuery("findAllSubscribers").getResultList();
//		for (Subscriber s : theSubscribers)
//			result += s.toString()+ "\n";
//		return result;
//	}
}
